package com.dsl.controller;

import com.dsl.entity.Event;
import com.dsl.entity.Page;
import com.dsl.entity.User;
import com.dsl.event.EventProducer;
import com.dsl.service.AttentionService;
import com.dsl.service.UserService;
import com.dsl.utils.CommunityConstant;
import com.dsl.utils.CommunityUtil;
import com.dsl.utils.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Description 关注控制器类
 * @Author dsl
 * @Date 2024/9/14 上午11:51
 * @Version 1.0
 */
@Controller
public class AttentionController implements CommunityConstant{

    @Autowired
    private AttentionService attentionService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private EventProducer eventProducer;

    //关注
    @PostMapping("/attention")
    @ResponseBody
    public String attention(int entityType, int entityId){
        User user = hostHolder.getUser();

        attentionService.attention(user.getId(), entityType, entityId);

        //触发关注事件
        Event event = new Event()
                .setTopic(TOPIC_ATTENTION)
                .setUserId(hostHolder.getUser().getId())
                .setEntityType(entityType)
                .setEntityId(entityId)
                .setEntityUserId(entityId);
        eventProducer.fireEvent(event);

        return CommunityUtil.getJSONString(0, "已关注！");
    }

    //取消关注
    @PostMapping("/unattention")
    @ResponseBody
    public String unattention(int entityType, int entityId){
        User user = hostHolder.getUser();

        attentionService.unattention(user.getId(), entityType, entityId);

        return CommunityUtil.getJSONString(0, "已取消关注！");
    }

    //查询某个用户关注的人
    @GetMapping("/attentions/{userId}")
    public String getAttention(@PathVariable("userId") int userId, Page page, Model model){
        User user = userService.getUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户不存在！");
        }
        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/attentions/" + userId);
        page.setRows((int)attentionService.getAttentionCount(userId, ENTITY_TYPE_USER));

        List<Map<String, Object>> userList = attentionService.getAttention(userId, page.getOffset(), page.getLimit());
        if(userList != null){
            for(Map<String, Object> map : userList){
                User u = (User)map.get("user");
                map.put("hasAttentioned", hasAttentioned(u.getId()));
            }
        }
        model.addAttribute("users", userList);

        return "/site/attention";
    }

    //查询某个用户的粉丝
    @GetMapping("/fans/{userId}")
    public String getFans(@PathVariable("userId") int userId, Page page, Model model){
        User user = userService.getUserById(userId);
        if(user == null){
            throw new RuntimeException("该用户不存在！");
        }
        model.addAttribute("user", user);

        page.setLimit(5);
        page.setPath("/fans/" + userId);
        page.setRows((int)attentionService.getFansCount(ENTITY_TYPE_USER, userId));

        List<Map<String, Object>> userList = attentionService.getFans(userId, page.getOffset(), page.getLimit());
        if(userList != null){
            for(Map<String, Object> map : userList){
                User u = (User)map.get("user");
                map.put("hasAttentioned", hasAttentioned(u.getId()));
            }
        }
        model.addAttribute("users", userList);

        return "/site/fan";
    }

    private boolean hasAttentioned(int userId){
        if(hostHolder.getUser() == null){
            return false;
        }

        return attentionService.hasAttentioned(hostHolder.getUser().getId(), CommunityConstant.ENTITY_TYPE_USER, userId);
    }
}
